package com.boy.rpc.framework.console.dao;


import com.boy.rpc.framework.console.Constant;
import com.boy.rpc.framework.console.exception.RedisConnectionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisTemplateFactory {
	
	private static Logger log = LoggerFactory.getLogger(RedisTemplateFactory.class);
	
	protected static RedisTemplate<String, Object> getRedisTemplate(String redisName) {
		RedisTemplate<String, Object> redisTemplate = Constant.redisTemplatesMap.get(redisName);
		if(redisTemplate==null) {
			log.error("redisTemplate==null" + ". had not connected to " + redisName + " this redis server now.");
			throw new RedisConnectionException("had not connected to " + redisName + " this redis server now.");
		}
		return redisTemplate;
	}
	
	private static void validate(int dbIndex) {
		if(0> dbIndex || dbIndex> 15) {
			log.error("0> dbIndex || dbIndex> 15" + "redis dbIndex is invalid : " + dbIndex);
			throw new RedisConnectionException("redis dbIndex is invalid : " + dbIndex);
		}
		return ;
	}
	
}
